home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2541 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.3 KB

  1. Path: info.spt.net.cn!usenet
  2. From: Xu Ji <jafd@public.sta.net.cn>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q: Returning a reference
  5. Date: Mon, 15 Jan 1996 09:58:57 -0800
  6. Organization: ZheJiang Securities Co.Ltd
  7. Message-ID: <30FA95E1.50C@public.sta.net.cn>
  8. References: <4cvsm2$5ig@dub-news-svc-4.compuserve.com>
  9. NNTP-Posting-Host: ts1-20.sta.net.cn
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b5 (Win16; I)
  14.  
  15. Martin Aupperle wrote:
  16. : Borland C++ V4.5 does not allow to return a reference to a local
  17. : variable:
  18. : int &doIt() {
  19. :   int i = 7;
  20. :   return i;  // syntax error
  21. :   }
  22. : I remember that I once had a compiler that did allow it (it gave me
  23. : only a warning).
  24. : Which one is right? I think that it should be an error because after
  25. : the function has terminated, the reference has no data object it is
  26. : bound to any more.
  27. : Martin
  28. : -----------------------------------
  29. : Signatures are a waste of bandwidth
  30. : -----------------------------------
  31.  
  32. I think it should be an error, and I test it under Borland C++
  33. and Watcom C++, both this two compiler report it as an error.
  34.  
  35. but the following function can be compiled:
  36.  
  37.  int &doIt()
  38.  {
  39.      int i = 7;
  40.      return (int &)i;
  41.  }
  42.  
  43. Best wishs,
  44. Xu yifeng
  45.